package com.vader.juc.cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author kaivader
 */
public class CAS {

    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void lock() {
        while (!atomicReference.compareAndSet(null, Thread.currentThread())) {
        }
        System.out.println("获取锁成功");
    }

    public void unLock(){
        while (!atomicReference.compareAndSet(Thread.currentThread(),null)){
        }
        System.out.println("释放锁成功");
    }
}
